import { IPagination } from './pagination'
import { IQueryParams } from './queryParams'
import { Contact } from './contact'
import { propertyMap } from '@shared/decorators/property-map'
import { User } from './user'
import { Address } from './address'
import { EPaymentBrand } from '@shared/enums'
import { Currency } from './common'
import { IBaseInfoColumn } from './baseInfo'

export enum EAuditConfigType {
  ExchangeRate = 'exchangeRate',
  RiskControl = 'riskControl',
  Store = 'store',
  TransRefund = 'transRefund',
  Operation = 'operation',
}

export interface IAcquirerQueryParams extends IQueryParams {
  startTime?: string
  endTime?: string
  insCode?: string
  insName?: string
  // more
}

export class Acquirer {
  institution: Institution
  qrChannels: QrChannel[] = []
  cardChannels: ICardChannel[] = []
  ecPayChannels: ECUPIChannel[] = []
  users: User[] = []

  constructor() {
    this.institution = new Institution()
  }
}

export class Institution {
  common: Common
  qr: AcquirerQr
  card: AcquirerCard
  version: number
  createdAt: string
  updatedAt: string

  constructor() {
    this.common = new Common()
    this.qr = new AcquirerQr()
    this.card = new AcquirerCard()
  }
}
// 渠道信息
export class QrChannel {
  id: string
  chanCode: EPaymentBrand // 渠道代码
  paymentBrand: EPaymentBrand // 支付品牌
  chanMerID?: string // 渠道商户号（支付宝：PID）
  chanMerName?: string // 渠道商户名称
  // key: string // 密钥
  currencies?: Currency[] // 币种
  fee?: number // 交易费率
  version?: number
  dynamicQrPrefix?: string
  staticQrPrefix?: string
  isSupportSettleCrr?: boolean
  publicKey?: string
  privateKey?: string
  isLoaded?: boolean // 是否是加载已有的渠道
}

export interface WXChannel extends QrChannel {
  signKey: string // API secret TODO
  publicKey: string // API Cetificate
  privateKey: string // API Cetificate 后台返回的字段
  privateKeyArray: string[] // 新增时使用的字段
  appID: string // WeChat APP ID
  isSupportPreAuth?: boolean
}

export interface ALPChannel extends QrChannel {
  signKey: string // MD5 Key
  publicKey: string // Alipay RSA Public Key
  privateKeyArray: string[] // RSA Private Key 新增时使用的字段
  privateKey: string // 后台返回的字段
  appID: string // alpAppId
  settleCurrency: string // Settlement Currency
  /** settlement 0, transaction 1 */
  settType: number
  sftp: SFTP
  isSupportPreAuth: boolean
}

export interface UPIChannel extends QrChannel {
  chanMerIDSecondary: string
  settleCurrency: string
  chanInsCode: string
  signKey: string
  encryptKey: string
  signKeyID: string
  encryptKeyID: string
}
export class CardChannel extends QrChannel {
  /**
   * 接入模式: Offline System
   */
  accessMode: EAccessMode

  /**
   * the Institution Code in the Offline System
   */
  offlineInsCode: string

  /**
   * the Channel Institution Code in the Offline System
   */
  offlineChannelInsCode: string

  /**
   * 线路地址IP
   */
  lineIP: string
  /**
   * 线路端口port
   */
  port: number
}

export interface ICardChannel extends QrChannel {
  /**
   * 接入模式: Offline System
   */
  accessMode?: EAccessMode

  /**
   * the Institution Code in the Offline System
   */
  offlineInsCode?: string

  /**
   * the Channel Institution Code in the Offline System
   */
  offlineChannelInsCode?: string

  /**
   * 线路地址IP
   */
  lineIP?: string
  /**
   * 线路端口port
   */
  port?: number
}

export interface ICardChannelVisa extends ICardChannel {
  bin: string
}

export interface ICardChannelMasterCard extends ICardChannel {
  bin: string
  ica: string
}

export interface ICardChannelAmex extends ICardChannel {
  mcc?: string
  chanAddrEn?: string
  chanNameEN?: string
  first3DigitsOfSENumber?: string
  ain?: string
}
export interface ICardChannelAmericanExpress extends ICardChannel {
  /**
   * ACquirer Identifier Number, assigned by AME
   */
  ain: number

  /**
   * First 3 digits of S/E Number
   */
  first3DigitsOfSENumber: number
}

export interface ICardChannelUpi extends ICardChannel {
  mcc?: string
  currency?: string
  fin?: string
  ain?: string
}

export interface ICardChannelUpiExpress extends ICardChannel {
  fin: string
  ain: string
  remark: string
  chanMerName: string
}

export class AcquirerQr {
  isRefundAsync = false
  isSupportTips = false
  isCollectQRCodeAmountFixed = false
  tradeTypes: string[] = []
  autoAlpApply = false
  autoWxpApply = false

  constructor() {}
}

export class AcquirerCard {
  isRefundAsync = false
  tradeTypes: string[] = []
}

export class Common {
  /* 新接口 */
  public insCode: string // 机构编号
  public insNameEN: string // 机构名称(英文)

  @propertyMap('insFullName')
  public insName: string // 机构名称（本地）
  public contact: Contact // 联系人信息
  public address: Address // 机构地址
  public businessArea: string // 主要业务区域
  public currencies: Currency[] = [] // 交易币种
  public settleInfo: Settle
  public auditConfigs: AuditConfig[] = []
  public feature: MerchantFeature
  public tradeInfo: TradeInfo
  public status: string

  // TODO: 清算信息还未处理
  constructor() {
    this.contact = new Contact()
    this.address = new Address()
    this.settleInfo = new Settle()
    this.tradeInfo = new TradeInfo()
    this.feature = new MerchantFeature()
  }
}

export class Settle {
  reportList: string[] = []
}

export class Step {
  stepID: number
  stepName: string
}

export class AuditConfig {
  type: EAuditConfigType
  stepNumber: number
  steps?: Step[] = []
}

export class MerchantFeature {
  multiTransCurrConfig: boolean
  isSupportRisk: boolean
  isSupportPostRisk: boolean
  isSupportDualMessage = false
}

export class TradeInfo {
  exchangeRates: ExchangeRate[] = []
  access: Access[] = []

  constructor() {}
}

export enum AccessMode {
  Merchant = 'merchant',
  Acquirer = 'acquirer',
  Node = 'node',
}

export class Access {
  mode: AccessMode
  key?: string
}

// 币种转换信息
export class ExchangeRate {
  // id: string
  srcCurrency: string
  dstCurrency: string
  buyRate: number
  selRate?: number
  markup: number
  rateSponsor: string // 转换汇率名称
  isFixed?: boolean // 固定值
  isBest?: boolean // 最优值

  createdAt?: string
  updatedAt?: string
  effectedAt?: string
}

export interface IExchangeRateInput {
  rateSponsor?: string
  selRate: number | string
  isFixed?: boolean
  isBest?: boolean
}

export interface SFTP {
  username: string
  password: string
  filePath: string
}

// 扫码配置
export class QRConfig {
  autoAlpApply: boolean
  autoWxpApply: boolean
  isCollectQRCodeAmountFixed: boolean
  isRefundAsync: boolean
  isSupportTips: boolean
  tradeTypes: string[] = []
}

// 卡配置
export class CardConfig {
  isRefundAsync: boolean
  tradeTypes: string[] = []
}

export interface IAcquirerPagination extends IPagination {
  data: Acquirer[]
}

export interface IInstitutionPagination extends IPagination {
  data: Institution[]
}

export interface ICommonPagination extends IPagination {
  data: Common[]
}

// 更新基础信息请求体
export interface IUpdateAcquirerBaseInfo {
  version: number
  common: {
    insName?: string;
    insNameEN?: string;
    contact?: Contact;
    address?: Address;
    businessArea?: string;
    currencies?: Currency[];
  }
}

// 更新交易信息请求体
export interface IUpdateAcquirerTradeInfo {
  version: number
  trade: TradeInfo
}

// 更新商户功能配置请求体
export interface IUpdateAcquirerFeature {
  version: number
  feature: MerchantFeature
}

// 更新 QR 功能信息请求体
export interface IUpdateAcquirerQRFeature {
  version: number
  feature: AcquirerQr
}

// 更新 卡功能信息请求体
export interface IUpdateAcquirerCardFeature {
  version: number
  feature: AcquirerCard
}

// 更新审核请求体
export interface IUpdateAcquirerAudit {
  version: number
  auditConfigs: AuditConfig[]
}

// 更新 清算信息请求体
export interface IUpdateAcquirerSettlement {
  version: number
  settlement: Settle
}

// 详情渠道信息
export interface IChannelInfo {
  imgUrl: string
  channelName?: string /** 渠道名称 */
  channelColumns: Array<IBaseInfoColumn[]>
}

export enum EAccessMode {
  CIL = 'OFI', // 讯联线下
  AME = 'AME', // 美运
  HKG = 'HKG', // 香港节点
  UPI = 'UPI',
}

export const AccessModeMap = {
  OFI: 'Offline System',
  AME: 'American Express Network Service',
  HKG: 'Hong Kong Gateway',
  UPI: 'UPI Network Service',
}

export class ECChannel extends QrChannel {
  acquiringIIN?: string // AcquiringIIN
  chanEncryptPubKeyId: string
  chanEncryptPubKey: string
  chanSignPubKeyId: string
  chanSignPubKey: string
  cilEncryPriKeyId: string
  cilEncryPriKey: string
  cilSignPriKeyId: string
  cilSignPriKey: string
}

export class ECUPIChannel extends ECChannel {
  acquiringIIN?: string // AcquiringIIN
  chanEncryptPubKeyId: string
  chanEncryptPubKey: string
  chanSignPubKeyId: string
  chanSignPubKey: string
  cilEncryPriKeyId: string
  cilEncryPriKey: string
  cilSignPriKeyId: string
  cilSignPriKey: string
}

export class ECVISChannel extends ECChannel {
  chanMerId: string
}

export class ECMCCChannel extends ECChannel {
  chanMerId: string
}

export class ECJCBChannel extends ECChannel {
  chanMerId: string
}

export interface IECUPIChannel extends ECChannel {
  acquiringIIN?: string // AcquiringIIN
  upiAcquiringIIN?: string
  chanMerId?: string
}
